function iframeOnload(aEvent)
{
	try
	{
		var persist = Components.classes["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
					  .createInstance(Components.interfaces.nsIWebBrowserPersist);
		var file = Components.classes["@mozilla.org/file/local;1"]
				   .createInstance(Components.interfaces.nsILocalFile);
		file.initWithPath("f:\\yaoyi.html"); // download destination
		//persist.persistFlags &= ~Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_NO_CONVERSION; // don't save gzipped
		persist.persistFlags &= ~Components.interfaces.nsIWebBrowserPersist.PERSIST_FLAGS_NO_BASE_TAG_MODIFICATIONS; // don't save gzipped
		var doc = aEvent.originalTarget;
		var baseTag = doc.createElement("base");
		var a = document.createAttribute("href");
		a.nodeValue = "a";
		baseTag.setAttributeNode(a);
		doc.getElementsByTagName("head")[0].appendChild(baseTag);
		persist.saveDocument(aEvent.originalTarget, file, null, null, null, null);
		alert("save ok");
	}
	catch(err)
	{
		alert(err);
	}	
}

function onLoad() {
	donkeybrowser = document.getElementById("donkey-browser");
	if (donkeybrowser) {
		donkeybrowser.style.height = "0px";
		donkeybrowser.webNavigation.allowAuth = true;
		donkeybrowser.webNavigation.allowImages = false;
		donkeybrowser.webNavigation.allowJavascript = false;
		donkeybrowser.webNavigation.allowMetaRedirects = true;
		donkeybrowser.webNavigation.allowPlugins = false;
		donkeybrowser.webNavigation.allowSubframes = false;
		donkeybrowser.addEventListener("DOMContentLoaded", iframeOnload, true);
	}
}

window.addEventListener("load", onLoad, true);

function siteGrabDump(obj)
{
	s = "";
	for(m in obj)
	{
		if("function" != typeof(obj[m]))
		{
			s = s + m + " " + obj[m] + "\n";
		}
		else
		{
			s = s + m + "\n"
		}
	}
	alert(s);
}


function Site_spider(browser)
{
	this.WaitSpideList = [];
	this.AlreadySpideList = [];
	
	Site_spider.prototype.spideDocument = function(doc)
	{
		try
		{
			this.AlreadySpideList.push(doc.location.href);
			var aTags = doc.getElementsByTagName("a");
			var i = 0;
			for(i = 0; i < aTags.length; i++)
			{
				var href = aTags[i].href;
				if( this.inWaitSpideList(href) )
				{
					continue;
				}
				
				if(this.inAlreadySpideList(href))
				{
					continue;
				}
				
				this.WaitSpideList.push(href);
			}
			
			
		}
		catch(err)
		{
			alert(err);
		}
	};
	
	Site_spider.prototype.inWaitSpideList = function(href)
	{
		return this.inList(href, this.WaitSpideList);
	}
	
	Site_spider.prototype.inAlreadySpideList = function(href)
	{
		return this.inList(href, this.AlreadySpideList);
	}
	
	Site_spider.prototype.inList = function(href, list)
	{
		var i = 0;
		for(i = 0; i < list.length; i++)
		{
			if(href == list[i])
			{
				return true;
			}
		}
		
		return false;
	}
}

function saveWebPage(doc)
{
	
}

function siteGrabGetUrl(event)
{
	if("undefined" == typeof(gBrowser.contentDocument.location.href) || "" == gBrowser.contentDocument.location.href || "about:blank" == gBrowser.contentDocument.location.href)
	{
		alert("please enter the web site! gBrowser.contentDocument.location.href: " + gBrowser.contentDocument.location.href + " !");
		return -1;
	}
	
	var siteGrabWaitSaveList = [];
	var siteGrabDoc = gBrowser.contentDocument;
	var siteGrabATagArray = siteGrabDoc.getElementsByTagName("a");
	
	var i = 0;
	for(i = 0; i < siteGrabATagArray.length; i++)
	{
		var j = 0;
		var find = false;
		var href = siteGrabATagArray[i].href;
		for(j = 0; j < siteGrabWaitSaveList.length; j++)
		{
			if(href == siteGrabWaitSaveList[j])
			{
				find = true;
				break;
			}
		}
		
		if(true == find)
		{
			continue;
		}
		
		siteGrabWaitSaveList.push(href);
	}

	gBrowser.siteGrabWaitSaveList = siteGrabWaitSaveList; 
	
	gBrowser.siteGrabAlreadySaveList = [gBrowser.contentDocument.location.href];
	gBrowser.spiderRootUrl = gBrowser.contentDocument.location.href;
	alert("begin spider!");
}
